package yao.util.personal.ddl;

import java.io.BufferedReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import yao.util.personal.ddl.Field.FType;



/**
 * 根据DDL语句生成 javaBean 和对应字段
 * @author chenyao
 *
 */
public class FieldCreator {
	
	private String DDL;
	private List<Field> fields;

	public FieldCreator(String filePath) {
		readToDDL(filePath);
	}
	private void readToDDL(String filePath) {
		//打开文件
		StringBuilder builder = new StringBuilder();
		Path path = Paths.get(filePath);
		//这里选择的编码要正确，不然会抛出 java.nio.charset.MalformedInputException: Input length = 1 异常
		try(BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
			String line;
			while((line = reader.readLine()) != null) {
				builder.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		this.DDL = builder.toString();
		this.fields = this.create();
	}
	
	public void print() {
		for(Field f : this.fields) {
			String str = "private " + f.getType().getJavaType() + " " + f.getName() + ";";
			System.out.println(str);
		}
	}
	public void printFieldNames() {
		StringBuilder builder = new StringBuilder();
		for(Field f : this.fields) {
			builder.append(f.getDdlName());
			builder.append(",");
		}
		String names = builder.toString();
		//去掉最后一个标点
		names = names.substring(0, names.length()-1);
		System.out.println(names);
	}

	public static void main(String[] args) {
		String filePath = "D:\\ddl.txt";
		FieldCreator f = new FieldCreator(filePath);
		f.print();
		System.out.println("----------------------------------------------");
		f.printFieldNames();
	}

	private List<Field> create() {
		List<Field> fields = new ArrayList<Field>();
		String[] field_str = this.getDDLFields();
		for(String ddlField : field_str) {
			ddlField = ddlField.trim();
			if(ddlField.indexOf("`") != 0) {
				continue;
			}
			Field createField = createField(ddlField);			
			fields.add(createField);
		}		
		return fields;
	}
	
	private String[] getDDLFields() {
		int start = this.DDL.indexOf("(");
		int end = this.DDL.lastIndexOf(")");
		String fieldsStr = this.DDL.substring(start + 1, end);
		return fieldsStr.split(",");
	}
	
	
	private Field createField(String ddlField) {
		ddlField = ddlField.trim();
		if(ddlField.indexOf("`") != 0) {
			return null;
		}
		Field field = new Field();
		String field_name = ddlField.substring(ddlField.indexOf("`") + 1, ddlField.lastIndexOf("`"));
		field.setDdlName(field_name);
		//转换为java形式书写
		String field_name_java = convertName(field_name);		
		field.setName(field_name_java);
		String[] split = ddlField.split(" ");
		if(split.length < 2) {
			throw new RuntimeException("拆分字段失败，str=" + ddlField);
		}
		String type_str = split[1];
		if(type_str.indexOf("(") >= 0) {
			type_str = type_str.substring(0, type_str.indexOf("("));
		}		
		FType type = FType.getType(type_str);
		if(type == null) {
			throw new RuntimeException("获取类型失败，type_str=" + type_str);
		}
		field.setType(type);
		return field;
	}
	
	 //首字母大写
    private String captureName(String name) {
    	if(name == null || name.length() <= 0) {
    		throw new RuntimeException("首字母大写转换失败");
    	}
        name = name.substring(0, 1).toUpperCase() + name.substring(1);
       return  name;      
    }
	
    private String convertName(String content) {
    	if(content == null || content.length() <= 0) {
    		throw new RuntimeException("content is null");
    	}
    	StringBuilder builder = new StringBuilder();
    	String[] words = content.split("_");
    	for(int i = 0; i < words.length; i++) {
    		String word = words[i];
    		if(i != 0) {
    			word = captureName(words[i]);
    		}
    		builder.append(word);
    	}   	
    	return builder.toString();
    }
}
